import cv2 as cv
import numpy as np

# 遍历所有的像素点，更改像素值
def access_pixels(image):  # pixel像素
    print(image.shape)
    height = image.shape[0]
    width = image.shape[1]
    channels = image.shape[2]
    print("width: %s  height: %s  channels: %s" % (width, height, channels))
    for row in range(height):
        for col in range(width):
            for c in range(channels):
                image[row, col, c] = 255 - image[row, col, c]
    cv.imshow("pixel_demo", image)


# 直接调用API取反
def inverse(image):
    img = cv.bitwise_not(image)  # 函数cv.bitwise_not可以实现像素点各通道值取反
    cv.imshow("inverse_demo", img)


# 自定义一个三通道的图片
def create_image():
    img = np.zeros([400, 400, 3], np.uint8)  # 赋予图像大小，将所有像素点的各通道数值赋0
    img[:, :, 0] = np.ones([400, 400])*255  # 0通道代表了B，将所有像素点的各通道数值赋0，再乘以255则变为蓝色
    # img[:, :, 1] = np.ones([400, 400]) * 255  # 1通道代表了G
    # img[:, :, 2] = np.ones([400, 400]) * 255  # 2通道代表了R
    cv.imshow("new_image", img)
    # img = np.zeros([400, 400, 1], np.uint8)  # 单通道
    # img[:, :, 0] = np.ones([400, 400])*127  # 得到一幅灰色的图像
    # 也可以将图像初始化为像素值全是1的图像，然后使用img = img * 127得到灰色的图像


def create_shuzu():
    n1 = np.ones([3, 3], np.uint8)  # 创建一个3*3
    n1.fill(25)  # 将数组中的每一个元素赋值为25。注意这里的数值不能超过255，当数值是256时，输出为0，当数值为257时输出为1
    print(n1)
    n2 = n1.reshape([1, 9])
    print(n2)
    n3 = np.array([[2, 3, 4], [4, 5, 6], [6, 7, 8]], np.uint8)
    n3.fill(9)
    print(n3)


src = cv.imread("imgs/test001.png")
cv.namedWindow("wangbobo", cv.WINDOW_AUTOSIZE)
cv.imshow("wangbobo", src)
t1 = cv.getTickCount()  # 用于返回从操作系统启动到当前所经毫秒数
create_shuzu()
t2 = cv.getTickCount()
time = (t2 - t1)/cv.getTickFrequency()  # getTickFrequency返回CPU的频率,就是每秒的计时周期数
print("time : %s ms" % (time*1000))
cv.waitKey(0)

cv.destroyAllWindows()
